<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title>管理员登录 - 聊天室后台</title>
  <style>
    * {
      box-sizing: border-box;
    }

    body {
      margin: 0;
      height: 100vh;
      background: linear-gradient(135deg, #74ebd5, #9face6);
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .login-box {
      background: rgba(255, 255, 255, 0.95);
      padding: 40px 30px;
      border-radius: 16px;
      box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
      width: 350px;
      animation: fadeIn 0.8s ease;
    }

    .login-box h2 {
      text-align: center;
      margin-bottom: 24px;
      font-size: 24px;
      color: #333;
    }

    .form-group {
      margin-bottom: 20px;
    }

    .form-group label {
      display: block;
      margin-bottom: 6px;
      font-weight: bold;
      color: #444;
    }

    .form-group input {
      width: 100%;
      padding: 10px 12px;
      border: 1px solid #ccc;
      border-radius: 8px;
      font-size: 14px;
      transition: border-color 0.3s;
    }

    .form-group input:focus {
      border-color: #409eff;
      outline: none;
    }

    .login-btn {
      width: 100%;
      padding: 12px;
      background: linear-gradient(90deg, #409eff, #66b1ff);
      border: none;
      color: white;
      font-size: 16px;
      font-weight: bold;
      border-radius: 8px;
      cursor: pointer;
      transition: background 0.3s ease;
    }

    .login-btn:hover {
      background: linear-gradient(90deg, #66b1ff, #409eff);
    }

    .error-message {
      color: #e74c3c;
      margin-top: 12px;
      text-align: center;
      display: none;
      font-size: 14px;
    }
  .custom-alert {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%) translateY(-100%);
    background-color: #fff;
    border: 1px solid #ccc;
    padding: 1em 1.5em;
    box-shadow: 0 2px 10px rgba(0,0,0,0.2);
    border-radius: 8px;
    z-index: 9999;
    transition: all 0.3s ease;
  }

  .custom-alert.success {
    border-color: #4caf50;
    background-color: #e8f5e9;
    color: #2e7d32;
  }

  .custom-alert.error {
    border-color: #f44336;
    background-color: #ffebee;
    color: #c62828;
  }

  .alert-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  #alertClose {
    background: none;
    border: none;
    font-size: 1.2em;
    cursor: pointer;
  }


    @keyframes fadeIn {
      from { opacity: 0; transform: translateY(-20px); }
      to { opacity: 1; transform: translateY(0); }
    }
  </style>
</head>
<body>
  <div class="login-box">
    <h2>管理员登录</h2>
    <div class="form-group">
      <label for="username">用户名</label>
      <input type="text" id="username" placeholder="请输入用户名">
    </div>
    <div class="form-group">
      <label for="password">密码</label>
      <input type="password" id="password" placeholder="请输入密码">
    </div>
    <button class="login-btn" onclick="handleLogin()">登录</button>
    <div class="error-message" id="errorMessage">用户名或密码错误</div>
    <!-- 自定义弹框 -->
<div id="customAlert" class="custom-alert" style="display: none; opacity: 0; transform: translateY(-100%); transition: all 0.3s;">
  <div class="alert-content">
    <span id="alertMessage"></span>
    <button id="alertClose">&times;</button>
  </div>
</div>

  </div>

  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script>
      // 显示自定义弹框
      function showAlert(message, type = 'info') {
          const alertBox = document.getElementById('customAlert');
          const alertMessage = document.getElementById('alertMessage');
          alertMessage.textContent = message;
          alertBox.className = `custom-alert ${type}`;
          alertBox.style.display = 'block';
          setTimeout(() => {
              alertBox.style.opacity = '1';
              alertBox.style.transform = 'translateY(0)';
          }, 10);
      }
  
      // 隐藏弹框（你可以选择保留或略去这个函数）
      function hideAlert() {
          const alertBox = document.getElementById('customAlert');
          alertBox.style.opacity = '0';
          alertBox.style.transform = 'translateY(-100%)';
          setTimeout(() => {
              alertBox.style.display = 'none';
          }, 300);
      }
  
      document.getElementById('alertClose')?.addEventListener('click', hideAlert);
  
      // 管理员登录逻辑
      function handleLogin() {
          const adminUsername = $('#username').val().trim();
          const adminPassword = $('#password').val().trim();
  
          if (!adminUsername || !adminPassword) {
              showAlert('请输入用户名和密码！', 'error');
              return;
          }
  
          $.ajax({
              url: '/adminLogin',
              type: 'POST',
              data: {
                adminUsername: adminUsername,
                adminPassword: adminPassword
              },
              success: function(response) {
                  if (response && response.adminId > 0) {
                      showAlert('登录成功!', 'success');
                      setTimeout(() => {
                          window.location.href = '/back.html';
                      }, 1500);
                  } else {
                      showAlert('登录失败，请检查用户名或密码。', 'error');
                  }
              },
              error: function() {
                  showAlert('登录请求失败，请稍后重试。', 'error');
              }
          });
      }
  
      // 绑定管理员登录（例如绑定某个按钮）
      $('#adminLoginBtn').on('click', function(e) {
          e.preventDefault();
          handleLogin();
      });
  </script>
  
</body>
</html>
